package com.sys.service.impl;

import com.bhudy.base.BaseServiceImpl;
import com.bhudy.base.QueryFilter;
import com.sys.entity.SysPlugin;
import com.sys.entity.SysPluginData;
import com.sys.service.SysPluginService;
import com.sys.utils.AesCBCUtil;
import com.bhudy.exception.SysException;
import com.bhudy.exception.SysExceptionCode;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author com.bhudy
 * @Description SysPluginService
 * @Date Tue Nov 05 23:15:20 CST 2019
 */
@Service
public class SysPluginServiceImpl extends BaseServiceImpl<SysPlugin, Serializable> implements SysPluginService {

    @Override
    public Map<String, String> getSysPluginDataByType(Integer type) {
        QueryFilter queryFilter = new QueryFilter();
        queryFilter.clear().and("type", type).and("unique", 0).and("status", 0);
        SysPlugin sysPlugin = super.find(queryFilter);

        if (sysPlugin == null) throw new SysException(SysExceptionCode.CODE_40002);

        queryFilter.clear().and("sysPluginId", sysPlugin.getId());
        List<SysPluginData> sysPluginDataList = super.findList(SysPluginData.class, queryFilter);
        if (sysPluginDataList == null || sysPluginDataList.size() == 0) throw new SysException(SysExceptionCode.CODE_40002);

        Map<String, String> reslutMap = new HashMap();
        for (SysPluginData sysPluginData : sysPluginDataList){
            String value = sysPluginData.getValue();
            if (sysPluginData.getIsEncrypt()== 1){
                // 解密
                value = AesCBCUtil.decrypt(value);
            }
            reslutMap.put(sysPluginData.getKey(), value);
        }

        if (reslutMap == null) throw new SysException(SysExceptionCode.CODE_40040);
        return reslutMap;
    }
}
